/**
 * Created by dell on 2017/3/4.
 */
$(function () {
  	var bdbBandOrRealse = {
		/* 初始数据 */
		defData: {
			pageSize: 20,
			serverGroupId: bdbMain.getUrlParams().id
		},
		pageState: {
			page:1,
			search:'',
			searchType:'ip',
		  	type: 'bindingIp',
		    noData: 'def'
		},

		/* 绑定或者释放IP菜单按钮 */
		bindOrRealseBtn: function() {
			var _this = this;
			$('.subMenu').off('click').on('click', 'span', function(e) {
				if ($(e.target).hasClass('releaseIP')) {
				    _this.pageState.type = 'releaseIp';
					$('.newItems').css('display','none');
					$('.releaseIP').removeClass('default');
					$('.bindIP').addClass('default');
					$('.bdb-bandRelaseFirm-btn div').text('释放IP');
					$('.releaseIP i').show();
				    $('.bindIP i').hide();

				} else {
				    _this.pageState.type = 'bindingIp';
					$('.releaseIP').addClass('default');
					$('.bindIP').removeClass('default');
					$('.bdb-bandRelaseFirm-btn div').text('绑定IP');
				    $('.bindIP i').show();
				    $('.releaseIP i').hide();
				}
			    $('.bdb-table-header .check-all-icon').removeClass('checkAll').addClass('checkNone');
			    _this.list();
				_this.bingOrRelaseSel();
			});
		},

		//绑定或释放ip
		bingOrRelaseSel: function() {
			var _this = this,
				type = _this.pageState.type;
			$('.bdb-bandRelaseFirm-btn div').off('click').on('click',function() {
				if ($('.bdb-list .check-icon.checkYes').length <= 0) {
					bdbMain.mLRManag({alert: {type: 2, msg: '请选择要操作的记录行!'}});
					return ;
				}

				if (type == 'bindingIp') {
				    _this.bindIpEvent();
				} else if(type == 'releaseIp') {
				    _this.relaseIpEvent();
				}
			});
		},

		//绑定ip
		bindIpEvent: function() {
			var _this = this,
				ipIdAry = [],
				ipIdList = '';

			if ($(".bdb-list-row .checkYes").length > 0) {
				$(".bdb-list-row .checkYes").each(function(i, n) {
				    ipIdAry.push($(n).parent().attr('data-id'));
				});
			    ipIdList = ipIdAry.join(',');
			}
		    $('.bdb-table-header .check-all-icon').removeClass('checkAll').addClass('checkNone');
			if (!bdbMain.ajaxInfo.state) {
				bdbMain.mLRManag({alert: {type: 2, msg: bdbMain.ajaxInfo.text}});
				return;
			}

			bdbMain.ajaxInfo.state = 0;
			$.post('/ServerGroup/bindingServerIp',{
				server_role_id: _this.defData.serverGroupId,
				ip_id: ipIdList
			}, function(response) {
				if (response.code == 0) {
					bdbMain.mLRManag({maskDown: 99, alert: {type: 1, msg: '绑定成功'}});
					_this.pageState.type = 'bindingIp';
				    $('.bdb-list-row').length == $(".bdb-list-row .checkYes").length ? _this.list({page: _this.pageState.page - 1}) : _this.list({page: _this.pageState.page});
					$('.bdb-table-header .check-all-icon').removeClass('checkAll').addClass('checkNone');
					$('.newItems').css('display','inline-block');
				} else {
				    bdbMain.mLRManag({partMask: false, loading: false, alert: {type: 2, msg: response.msg}});
				}
			    bdbMain.ajaxInfo.state = 1;
			});

		},

		//释放ip
		relaseIpEvent: function() {
			var _this = this,
				ipIdAry = [],
				ipIdList = '';

			if ($(".bdb-list-row .checkYes").length > 0) {
				$(".bdb-list-row .checkYes").each(function(i, n) {
				  	ipIdAry.push($(n).parent().attr('data-id'));
				});
				ipIdList = ipIdAry.join(',');
			}

			if (!bdbMain.ajaxInfo.state) {
				bdbMain.mLRManag({alert: {type: 2, msg: bdbMain.ajaxInfo.text}});
				return;
			}

			bdbMain.ajaxInfo.state = 0;
			$.post('/ServerGroup/releaseServerIp', {
				server_role_id: _this.defData.serverGroupId,
				ip_id: ipIdList
			}, function(response) {
				if(response.code == 0) {
					bdbMain.mLRManag({maskDown: 99, alert:{type: 1, msg: '释放成功'}});
					_this.pageState.type = 'releaseIp';
				    $('.bdb-list-row').length == $(".bdb-list-row .checkYes").length ? _this.list({page: _this.pageState.page - 1}) : _this.list({page: _this.pageState.page});
				    $('.bdb-table-header .check-all-icon').removeClass('checkAll').addClass('checkNone');
				} else {
				  	bdbMain.mLRManag({partMask: false, loading: false, alert: {type: 2, msg: response.msg}});
				}
			    bdbMain.ajaxInfo.state = 1;
			});

		},

		//获取列表数据
		list: function(obj) {
			var _this = this,
				obj = obj || {page: 1};
				_this.pageState.page = obj.page;

				bdbMain.mLRManag({mask: false, partMask: true});

			$.post('/ServerGroup/listIP', {
				server_group_id: _this.defData.serverGroupId ,
				type: _this.pageState.type,
				query: _this.pageState.search,
				search_type: _this.pageState.searchType,
			    pageNumber: _this.pageState.page,
				pageSize: _this.defData.pageSize
			}, function(response) {
				var totalRowCnt = 0;
				if (response.code == 0) {
					bdbMain.mLRManag({partMask: false, loading: false});
					var listHtml = _this.listProc(response.data);
					totalRowCnt = response.data.totalCount;
					_this.listNull(totalRowCnt);
					$('.bdb-list').empty().append(listHtml);

					//分页插件展示
					$('#js-bdb-creatPage').bdbPagination({
						totalRowCnt: totalRowCnt,
						currentPage: _this.pageState.page,
						pageSize: _this.defData.pageSize
					});
					$('.bdb-record-cnt').html(bdbMain.pageInfo.recordCnt.replace('{cnt}', totalRowCnt));
				} else {
				    bdbMain.mLRManag({partMask: false, loading: false, alert: {type: 2, msg: response.msg}});
				}
			});
		},

		//生成列表模板
		listProc: function(data) {
			var tpl = '', ipTypeAry = ['', '虚拟IP', '真实IP', '单一IP', '管理IP'];

			$.each(data.list, function(index, value) {
				tpl += '<div class="bdb-list-row" data-id="' + value.ip_id + '">\
								<div class="bdb-mx-w-col1 check-icon checkNo"></div>\
								<div class="bdb-mx-w-col2 bdb-ellipsis">' + value.ip_id + '</div>\
								<div class="bdb-mx-w-col3 bdb-ellipsis">' + value.ip + '</div>\
								<div class="bdb-mx-w-col4 bdb-ellipsis">' + ipTypeAry[value.ip_type] + '</div>\
								<div class="bdb-mx-w-col5 bdb-ellipsis">' + value.hostname + '</div>\
								<div class="bdb-mx-w-col6 bdb-ellipsis">' + value.server_area + '</div>\
								</div>'
			});

			return tpl;
		},

		//暂无数据展示
		listNull: function(totalRowCnt) {
			var noResult = $('.bdb-no-result'),
				noSearchRecord = $('.bdb-no-search-record');

			if (totalRowCnt <= 0) {
				if (this.pageState.noData == 'search') { // 查询无结果
					noResult.hide();
					noSearchRecord.show();
				} else { // 暂无记录.
					noResult.show();
					noSearchRecord.hide();
				}
			} else {
				noResult.hide();
				noSearchRecord.hide();
			}
		},

		/* 加载分页 */
		pageEvent: function() {
			var _this = this;
			$('.bdb-creatPage').off('click').on('click', '.bdb-pagination', function (e) {
				e.stopPropagation();
				e.preventDefault();
				var page = parseInt($(e.target).text());

				if(!isNaN(page)){
					_this.list({page: page});
				    $('.check-all-icon').removeClass('checkAll').addClass('checkNone');
				}
			});
		},

		/* 搜索功能 */
		searchEvent: function() {
			var _this = this,
				searchInputEle = $('.bdb-search-input'),
				selectitem = $('.selectItem'),
				searchType = 'ip';

		  	$('.search-left-select').off('mouseenter').on('mouseenter', function() {
			  	selectitem.css('display','block');
			}).off('mouseleave').on('mouseleave', function() {
			    selectitem.css('display','none');
			});

			selectitem.off('click').on('click', 'li', function(e) {
				e.stopPropagation();
				$('.search-left-select').find('em').text($(e.target).text()).attr('data-type', $(e.target).attr('data-type'));
				searchType = $(e.target).attr('data-type');
				selectitem.css('display', 'none');
			});

			searchInputEle.off('focusin').on('focusin', '.search-text', function() {
				var _cThis = $(this),
					_btnEle = $(this).parent().find('.search-gray');

				if (!_btnEle.hasClass('search-clear')) {
				    _cThis.animate({width: '240px'}, 500);
					_btnEle.animate({ left : 240 }, 500, function() {
						_btnEle.removeClass('search-gray').addClass('search-blue');
					});
				}
			});

		  	searchInputEle.off('click').on('click', '.search-conf', function() {
		  	    var left = parseInt($(this).css('left'));
				if ($(this).hasClass('search-blue') && left > 0) {
					_this.pageState.search = searchInputEle.find('.search-text').val();
					_this.pageState.searchType = searchType;
					_this.pageState.noData = 'search';
				    $(this).removeClass('search-blue').addClass('search-clear');
					_this.list();
				} else if (left > 0){
					searchInputEle.find('.search-text').val('');
					_this.pageState.noData = 'def';
					_this.pageState.search = '';
					$(this).removeClass('search-clear').addClass('search-blue');
					_this.list();
				}
			});
		},

	  	/* 绑定释放ip确认按钮激活 */
	  	bandOrReleaseActive: function () {
	  	    var activeBtn = $('.bdb-bandRelaseFirm-btn > div');
			$('.bdb-container').bdbCheckBox({
				checked: function() {
					var rowSelCont = $('.bdb-list').find('.checkYes').length;

					if(rowSelCont >= 1) {
					  activeBtn.addClass('bdb-bandReleaseFirm-active');
					} else if (rowSelCont = 1) {
					  activeBtn.removeClass('bdb-bandReleaseFirm-active');
					}
				}
			});
		},

		/* 根据浏览器resize页面 */
		contentSizeResize: function() {
			$(window).resize(function() {
				$('.bdb-list').bdbScreenH(2);
			});
		},

		init: function() {
			var _this = this;

			_this.list();
			_this.bindOrRealseBtn();
			_this.bingOrRelaseSel('bindingIp');
			_this.searchEvent();
			_this.pageEvent();
			_this.contentSizeResize();
			_this.bandOrReleaseActive();

			$('.bdb-list').bdbScreenH(2);
		}
    };
	bdbBandOrRealse.init();
});